package com.southpark.demo.managers
{
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import com.southpark.demo.control.ApplicationController;
	import com.southpark.demo.control.CommandMap;
	import com.southpark.demo.control.events.config.GetConfigSettingsEvent;
	import com.southpark.demo.control.events.config.LoadThemeEvent;
	import com.southpark.demo.interfaces.model.IModel;
	import com.southpark.demo.interfaces.pages.IWelcomePage;
	import com.southpark.demo.interfaces.services.IServices;
	import com.southpark.demo.model.Model;
	import com.southpark.demo.services.Services;
	import com.southpark.demo.view.ViewMap;
	import com.southpark.demo.view.info.SystemErrorBoxUI;
	import com.southpark.demo.vo.config.ThemeVO;
	import com.southpark.framework.control.responders.IResponder;
	import com.southpark.framework.control.responders.ResponderFactory;
	import com.southpark.system.interfaces.application.IApplication;
	import com.southpark.system.interfaces.application.IBackground;
	import com.southpark.system.interfaces.info.ISystemErrorBox;
	import com.southpark.system.interfaces.managers.IApplicationManager;
	import com.southpark.system.interfaces.managers.IAssetManager;
	import com.southpark.system.interfaces.managers.ICookieManager;
	import com.southpark.system.interfaces.managers.IDeepLinkingManager;
	import com.southpark.system.interfaces.managers.IInfoManager;
	import com.southpark.system.interfaces.managers.IPageManager;
	import com.southpark.system.interfaces.managers.IPopUpManager;
	import com.southpark.system.interfaces.managers.ITextManager;
	import com.southpark.system.interfaces.managers.IToolTipManager;
	import com.southpark.system.managers.AssetManager;
	import com.southpark.system.managers.CookieManager;
	import com.southpark.system.managers.DeepLinkingManager;
	import com.southpark.system.managers.InfoManager;
	import com.southpark.system.managers.PageManager;
	import com.southpark.system.managers.PopUpManager;
	import com.southpark.system.managers.ToolTipManager;
	import com.southpark.system.view.debug.DebugPanel;
	
	import flash.events.ContextMenuEvent;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	
	import flexunit.flexui.TestRunnerBase;
	
	import lab.southpark.demo.tests.TestCases;
	
	import mx.core.Application;

	
	
	public class ApplicationManager implements IApplicationManager
	{	
		
		public function ApplicationManager(application:IApplication)
		{
			this.application = application;
		}

		
		/**
		 * 
		 */
		private var application:IApplication;
		private var applicationController:ApplicationController;
		
		 
		public function get applicationName():String
		{
			return "DEMO-APP";
		}

		public function get defaultPageClass():Class
		{
			return IWelcomePage;
		}
		
		public function get buildVersion():String
		{
			return application.buildVersion;
		}
		
		public function get background():IBackground
		{
			return application.background;
		}
		

		/**
		 * 
		 */
		private var _model:IModel;
		public function get model():Object
		{
			if (!_model) _model = new Model(this) as IModel;
			return _model;
		}
		
		private var _services:IServices;
		public function get services():Object
		{
			if (!_services) _services = new Services(this) as IServices;
			return _services;
		}
		
		
		
		/**
		 * 
		 */
		private var _assetManager:IAssetManager;
		public function get assetManager():IAssetManager
		{
			if (!_assetManager) _assetManager = new AssetManager(this) as IAssetManager;
			return _assetManager;
		}
		
		private var _cookieManager:ICookieManager;
		public function get cookieManager():ICookieManager
		{
			if (!_cookieManager) _cookieManager = new CookieManager(this) as ICookieManager;
			return _cookieManager;
		}
		
		private var _deepLinkingManager:IDeepLinkingManager;
		public function get deepLinkingManager():IDeepLinkingManager
		{
			if (!_deepLinkingManager) _deepLinkingManager = new DeepLinkingManager(this) as IDeepLinkingManager;
			return _deepLinkingManager;
		}
		
		private var _infoManager:IInfoManager;
		public function get infoManager():IInfoManager
		{
			if (!_infoManager) _infoManager = new InfoManager(this) as IInfoManager;
			return _infoManager;
		}
		
		private var _pageManager:IPageManager;
		public function get pageManager():IPageManager
		{
			if (!_pageManager) _pageManager = new PageManager(this) as IPageManager;
			return _pageManager;
		}
		
		private var _popUpManager:IPopUpManager;
		public function get popUpManager():IPopUpManager
		{
			if (!_popUpManager) _popUpManager = new PopUpManager(this) as IPopUpManager;
			return _popUpManager;
		}
		
		private var _textManager:ITextManager;
		public function get textManager():ITextManager
		{
			if (!_textManager) _textManager = new TextManager(this) as ITextManager;
			return _textManager;
		}
		
		private var _toolTipManager:IToolTipManager;
		public function get toolTipManager():IToolTipManager
		{
			if (!_toolTipManager) _toolTipManager = new ToolTipManager(this) as IToolTipManager;
			return _toolTipManager;
		}
		
		
		
		/**
		 * 
		 */
		public function addEventListener(type:String,listener:Function):void
		{
			CairngormEventDispatcher.getInstance().addEventListener(type,listener);
		}
		
		public function removeEventListener(type:String,listener:Function):void
		{
			CairngormEventDispatcher.getInstance().removeEventListener(type,listener);
		}
		
		
		
		
		/**
		 * initialize
		 */
		public function initialize():void
		{
			applicationController = new ApplicationController(this);
			
			IModel(model).config.buildVersion = application.buildVersion;
			
			var responder:IResponder = ResponderFactory.createResponder(onGetConfigSettingsSuccess);
			var e:GetConfigSettingsEvent = new GetConfigSettingsEvent(GetConfigSettingsEvent.GET_CONFIG_SETTINGS_EVENT,responder)
			e.dispatch();
		}
		 
		private function onGetConfigSettingsSuccess(result:Object):void
		{
			buildContexMenu();
			deepLinkingManager.start();
			
			if (IModel(model).config.isUnitTestMode)
			{
				application.background.visible = false;
				
				var testRunner:TestRunnerBase = new TestRunnerBase();
				testRunner.percentWidth = 100;
				testRunner.percentHeight = 100;
				testRunner.test = new TestCases();
				
				Application.application.addChild(testRunner);
            	testRunner.startTest();
				
				return;
			};
			
			if (IModel(model).config.themes && IModel(model).config.themes.length != 0)
			{
				var theme:ThemeVO = IModel(model).config.themes[0] as ThemeVO;
				if (theme)
				{
					var e:LoadThemeEvent = new LoadThemeEvent(LoadThemeEvent.LOAD_THEME_EVENT);
					e.source = theme.source;
					e.dispatch();
				};
			};

		}
		
		
		
		/**
		 * context menu
		 */
		private function buildContexMenu():void
		{
			var contextMenu:ContextMenu = new ContextMenu();
			
			contextMenu.hideBuiltInItems();
			contextMenu.customItems = new Array();
			
			
			//var contextMenuItem01:ContextMenuItem = new ContextMenuItem("Application Status");
			//var contextMenuItem02:ContextMenuItem = new ContextMenuItem("");
			//var contextMenuItem03:ContextMenuItem = new ContextMenuItem("");
		
			//contextMenuItem01.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,function():void{popUpManager.addPopUp(IApplicationStatusPopUp,true,false)});
			//contextMenuItem02.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,function():void{});
			//contextMenuItem03.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,function():void{});

			//contextMenu.customItems.push(contextMenuItem01);
			//contextMenu.customItems.push(contextMenuItem02);
			//contextMenu.customItems.push(contextMenuItem03);
			
			if (IModel(model).config.isDebugMode)
			{				
				var contextMenuItemExtra01:ContextMenuItem = new ContextMenuItem("Debug Panel",true);
				var contextMenuItemExtra02:ContextMenuItem = new ContextMenuItem("Test1");
				var contextMenuItemExtra03:ContextMenuItem = new ContextMenuItem("Test2");
				
				contextMenuItemExtra01.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,function():void{debugPanel.show()});
				contextMenuItemExtra02.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,function():void{});
				contextMenuItemExtra03.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,function():void{});
				
				contextMenu.customItems.push(contextMenuItemExtra01);
				contextMenu.customItems.push(contextMenuItemExtra02);
				contextMenu.customItems.push(contextMenuItemExtra03);
			};
		
			Application.application.contextMenu = contextMenu;
		}
		
		/**
		 * 
		 */
		public function get systemErrorBox():ISystemErrorBox
		{
			return new SystemErrorBoxUI() as ISystemErrorBox;
		} 
		
		public function get commandMap():Array
		{
			return CommandMap.COMMAND_MAP;
		}
		
		public function get pageMap():Array
		{
			return ViewMap.PAGE_MAP;
		}
		
		public function get popUpMap():Array
		{
			return ViewMap.POP_UP_MAP;
		}
		
		public function get assetMap():Array
		{
			return ViewMap.ASSET_MAP;
		}
		
		
	
		
		/**
		 * debug 
		 */
		private var _debugPanel:DebugPanel;
		private function get debugPanel():DebugPanel
		{
			if (!_debugPanel) _debugPanel = new DebugPanel();
			return _debugPanel;
		}
		
		public function get isDeployedLocally():Boolean
		{
			return Application.application.url.indexOf("localhost") != -1;
		}
		
		public function get isDebugMode():Boolean
		{
			return !(IModel(model).config.configFileVersion && !IModel(model).config.isDebugMode);
		}

		public function debug(key:String,value:String,details:String):void
		{
			if (isDebugMode)
			{
				debugPanel.addEntry(key,value,details);
			};
		}
		
		
		
	}
}